package com.ehotting.edsta.circuitDesign.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.ehotting.edsta.circuitDesign.domain.EsElement;
import com.ehotting.eaf.core.utils.poi.ExcelUtil;
import com.ehotting.eaf.core.web.controller.BaseController;
import com.ehotting.eaf.core.web.domain.AjaxResult;
import com.ehotting.eaf.core.web.page.TableDataInfo;
import com.ehotting.edsta.common.log.annotation.Log;
import com.ehotting.edsta.common.log.enums.BusinessType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ehotting.edsta.circuitDesign.service.EsElementService;
import org.springframework.web.multipart.MultipartFile;


/**
 * 元器件Controller
 * 
 * @author edsta
 * @date 2023-05-08
 */
@RestController
@RequestMapping("/element")
public class EsElementController extends BaseController
{
    @Autowired
    private EsElementService esComponentService;

    /**
     * 查询元器件列表
     */
//    @PreAuthorize(hasPermi = "edsta:element:list")
    @GetMapping("/list")
    public TableDataInfo list(EsElement esElement)
    {
        startPage();
        List<EsElement> list = esComponentService.selectEsElementList(esElement);
        return getDataTable(list);
    }

    /**
     * 导出元器件列表
     */
//    @PreAuthorize(hasPermi = "edsta:element:export")
    @Log(title = "元器件", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EsElement esElement) throws IOException
    {
        List<EsElement> list = esComponentService.selectEsElementList(esElement);
        ExcelUtil<EsElement> util = new ExcelUtil<EsElement>(EsElement.class);
        util.exportExcel(response, list, "元器件数据");
    }

    /**
     * 获取元器件详细信息
     */
//    @PreAuthorize(hasPermi = "edsta:element:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(esComponentService.selectEsElementById(id));
    }

    /**
     * 新增元器件
     */
//    @PreAuthorize(hasPermi = "edsta:element:add")
    @Log(title = "元器件", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EsElement esElement)
    {
        return toAjax(esComponentService.insertEsElement(esElement));
    }

    /**
     * 修改元器件
     */
//    @PreAuthorize(hasPermi = "edsta:element:edit")
    @Log(title = "元器件", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EsElement esElement)
    {
        return toAjax(esComponentService.updateEsElement(esElement));
    }

    /**
     * 删除元器件
     */
//    @PreAuthorize(hasPermi = "edsta:element:remove")
    @Log(title = "元器件", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(esComponentService.deleteEsElementByIds(ids));
    }

    /**
     * 导入元器件文件
     * @param file xlxs表格
     * @return
     */
    @PostMapping("/importExcel")
    public AjaxResult importExcel(@RequestParam MultipartFile file) {

        logger.info("" + file.getOriginalFilename());

        return toAjax(esComponentService.importExcel(file));
    }
}
